import datetime

from marshmallow import fields

from apps.extensions import db, ma
from flask_login import UserMixin


class CrawlerEntity(db.Model, UserMixin):
    __tablename__ = 'admin_crawler'
    id = db.Column(db.Integer, primary_key=True, autoincrement=True, comment='用户ID')
    name = db.Column(db.String(20), comment='用户名')
    sleep_time = db.Column(db.String(128), comment='哈希密码')
    use_proxy = db.Column(db.String(128), comment='哈希密码')
    user_agent = db.Column(db.String(128), comment='哈希密码')
    list_config = db.Column(db.String(1024), comment='哈希密码')
    detail_config = db.Column(db.String(1024), comment='哈希密码')
    db_config = db.Column(db.String(1024), comment='哈希密码')
    render_type = db.Column(db.String(64), comment='哈希密码')
    web_site = db.Column(db.String(128), comment='哈希密码')
    deleted = db.Column(db.Integer, default=0, comment='启用')
    create_time = db.Column(db.DateTime, default=datetime.datetime.now, comment='创建时间')
    update_time = db.Column(db.DateTime, default=datetime.datetime.now, onupdate=datetime.datetime.now, comment='创建时间')

    def save(self, param):
        db.session.add(param)
        db.session.up
        db.session.commit()
        return False


class CrawlerPageOut(ma.Schema):
    id = fields.Integer()
    name = fields.Str()
    sleep_time = fields.Str()
    use_proxy = fields.Str()
    web_site = fields.Str()
    render_type = fields.Str()
    create_at = fields.DateTime()
    update_at = fields.DateTime()


class CrawlerSaveIn(ma.Schema):
    id = fields.Integer()
    name = fields.Str()
    sleep_time = fields.Str()
    use_proxy = fields.Str()
    web_site = fields.Str()
    render_type = fields.Str()
    create_at = fields.DateTime()
    update_at = fields.DateTime()
